function [Val,V]=Fun_V(PP,SS,T,Profit,TAU,ir_dom,ir_ext,w_N,w_H,PortfolioInfo,UCoefp,State,Flag_SS)
% T=SS.T;Profit=SS.Profit;ir_dom=SS.ir_dom;ir_ext=SS.ir_ext;Pir=SS.Pir;
% w_N=SS.w_N;w_H=SS.w_H;State=SS.FunApp.V.State;TAU=SS.TAU;UCoefp=UCoef_0;
% PortfolioInfo=SS.PortfolioInfo;
if nargin<=12
    Flag_SS     =   0;
end
%% Preliminary
% States
N_State     =   size(State,1);
f           =   State(:,1);
ID          =   State(:,2);

ID_z        =   SS.FunApp.ExoState.State(ID,1);
ID_FI       =   SS.FunApp.ExoState.State(ID,2);
ID_RI       =   SS.FunApp.ExoState.State(ID,3);

z           =   PP.ExoState.IdioInc.Node(ID_z);
FI          =   PP.ExoState.Idio_FI.Node(ID_FI);
RI          =   PP.ExoState.Idio_RI.Node(ID_RI);

what        =   (1-TAU)*( RI*w_H + (1-RI)*w_N );

Eff_z       =   z.*(1+PP.WGap_FI(FI+1)).*(1+PP.WGap_RI(RI+1));
Eff_w       =   what.*Eff_z;
% Return rates for fin. integrated hhs
% PortfolioMat=   reshape(PortfolioInfo,[4,2]);
% 
% DomShare_Pos=   PortfolioMat(1,1);
% DomShare_Neg=   PortfolioMat(1,2);
% 
% ER_Pos      =   PortfolioMat(2,1);
% ER_Neg      =   PortfolioMat(2,2);
% 
% KAPPA_Pos   =   PortfolioMat(3,1);
% KAPPA_Neg   =   PortfolioMat(3,2);
% 
% FR_Pos      =   PortfolioMat(4,1);
% FR_Neg      =   PortfolioMat(4,2);
% 
% ret_pos     =   FI*ER_Pos + (1-FI)*ir_dom;
% ret_neg     =   FI*ER_Neg + (1-FI)*ir_dom+PP.KAPPA;
% 
% fc_pos      =   FI*KAPPA_Pos + (1-FI)*0;
% fc_neg      =   FI*KAPPA_Neg + (1-FI)*0 + PP.KAPPA;
% 
% domshare_pos=   FI*DomShare_Pos + (1-FI)*1;
% domshare_neg=   FI*DomShare_Neg + (1-FI)*1;
% 
% fr_pos      =   FI*FR_Pos + (1-FI)*ir_dom;
% fr_neg      =   FI*FR_Neg + (1-FI)*ir_dom;
Bp_ext      =   PortfolioInfo(1)*FI;
AdjCost     =   PortfolioInfo(2)*FI;
q_dom       =   PortfolioInfo(3);
q_ext       =   PortfolioInfo(4);
% Boundary for bp Imposed by Approximation Structure
fp_lb       =   SS.FunApp.U.UnitLimit(1,1);
fp_ub       =   SS.FunApp.U.UnitLimit(1,2);
% Important Constant
wN_SS       =   SS.w_N*SS.N_N+SS.w_H*SS.N_H;
x           =   T+Profit*z+f;

%% Determine the Potential UI
% Maximium Available Expenditure
b_low       =   (fp_lb - Bp_ext)*q_dom + Bp_ext*q_ext + AdjCost;
b_upp       =   (fp_ub - Bp_ext)*q_dom + Bp_ext*q_ext + AdjCost;

b_lb        =   max(b_low,PP.b_lb);
b_ub        =   max(b_upp,b_lb);
MaxExp      =   x-b_lb;
% Minimum Required Expenditure
[~,MinExp,~]=   UtilityFlow_Sep(PP.NU_C,PP.NU_L,SS.PSI,Eff_w,PP.MinCon*wN_SS,'Consumption');
MinExp      =   MinExp/wN_SS;
% Required UI Benefit
UI          =   (1+PP.UI_Slack)*max(MinExp-MaxExp,0);


%% Optimization
% Effective State: Available Wealth (x=c+f)
x           =   x+UI;
% Boundary 
Exp_lb      =   max(x-b_ub,MinExp);
Exp_ub      =   max(x-b_lb,Exp_lb);

[~,c_lb,~]  =   UtilityFlow_Sep(PP.NU_C,PP.NU_L,SS.PSI,Eff_w,Exp_lb*wN_SS,'Expenditure');
[~,c_ub,~]  =   UtilityFlow_Sep(PP.NU_C,PP.NU_L,SS.PSI,Eff_w,Exp_ub*wN_SS,'Expenditure');

c_lb        =   c_lb/wN_SS;
c_ub        =   c_ub/wN_SS;

% Optimization
TempFun     =   @(Policy,State) ...
                Fun_V_Eval(PP,SS,UCoefp,State,q_dom, q_ext,Policy,0);
TempState   =   [x,ID,what,Bp_ext,AdjCost];
[Opt_c,~]   =   VecFun_GoldenSearch(TempFun,TempState,c_lb,c_ub,1e-6);
  
[Val,V]     =   Fun_V_Eval(PP,SS,UCoefp,TempState,q_dom, q_ext, Opt_c,Flag_SS);
%% Collect the Extra Policy
V.Policy.FlagBC ...
            =   (V.Policy.c-c_ub)<1e-6;
V.Policy.UI =   UI;
V.Policy.FlagUI ...
            =   UI>0;

% Ind_Pos     =   V.Policy.b_fc>=0;
% Ind_Neg     =   ~Ind_Pos;
V.Policy.b  =   V.Policy.b_fc - AdjCost;
V.Policy.fc =   V.Policy.b_fc - V.Policy.b;
V.Policy.b_dom= V.Policy.b - Bp_ext*q_ext;
V.Policy.b_ext= V.Policy.b - V.Policy.b_dom;
V.Policy.bp_dom=V.Policy.b_dom/q_dom;
V.Policy.bp_ext=V.Policy.b_ext/q_ext;

